package student.aistar.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import student.aistar.mapper.ProductMapper;

import student.aistar.model.dto.ProductInfoDTO;
import student.aistar.model.entity.*;

import student.aistar.service.ICartService;
import student.aistar.service.IProductService;

import java.util.List;

/**
 * 本类用来演示:
 *
 * @author: qinFeng
 * @date: 2021/9/27 10:04
 */
@Service
public class ProductService implements IProductService {
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ICartService cartService;

    @Override
    public Product findById(Integer id) {
        return productMapper.selectProductBytId(id);
    }

    /**
     * 查询多个商品(商品Id, 通过字符串拼接)
     * @param productIds
     * @return
     */
    @Override
    public List<Product> getMoreProByStr(String productIds) {
        return null;
    }
    @Override

    public Evaluate getByproductId(Integer productId) {

        return productMapper.selectEvaluateByproductId(productId);
    }


    public List<Category> getAllCategory() {
        return productMapper.findAllCategory();
    }

    @Override
    public List<Product> getAllProduct() {
        return productMapper.findAllProduct();
    }

    @Override
    public PageInfo<Product> getProduct(String productName, Integer categoryId, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Product> productList = productMapper.findProduct(productName, categoryId);
        PageInfo<Product> pageInfo = new PageInfo<>(productList);
        return pageInfo;
    }

    /**
     * 处理
     * @param id
     * @return
     */
    @Override
    public ProductInfoDTO productInfo(Integer id) {
        return productMapper.productInfo(id);
    }


    /**
     * 根据点击购买的时候, 将数据插入到购物车,
     * 还要考虑用户没登陆的情况, 放到session作用域
     * @param user
     * @param productId
     * @param para
     * @param proName
     * @param paraPrice
     */
    @Override
    public void addSuccessTip(User user, Integer productId, String para, String proName, Double paraPrice) {
        if(user == null){

        }
        else{
            Integer userId = user.getUserId();
            Cart cart = cartService.selectByUserIdAndProductId(user.getUserId(), productId, para);
            if(cart == null){
                cartService.addCart(new Cart(null, userId, productId, 1, 0, 1, paraPrice, para, paraPrice));
            }else{
                Integer num = cart.getNum();
                cartService.updateCart(cart.getParaPrice(), num + 1,userId,  productId, para);
            }
        }
    }

    @Override
    public List<Overviewpage> selectOverViewPageByProductId(Integer productId) {
        return productMapper.selectOverViewPageByProductId(productId);
    }


}
